package com.casems.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.casems.entity.RolePermission;
import com.casems.mapper.RolePermissionMapper;
import com.casems.service.RolePermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tch
 * @since 2023-05-18
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements RolePermissionService {

    @Autowired
    RolePermissionMapper rolePermissionMapper;

    @Transactional
    @Override
    public boolean updateRolePermission(int roleid, int[] permissionid) {
        QueryWrapper deleteWrapper = new QueryWrapper();
        deleteWrapper.eq("role_id",roleid);
        rolePermissionMapper.delete(deleteWrapper);
        for(int i = 0; i < permissionid.length; i++) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleid);
            rolePermission.setPermissionId(permissionid[i]);
            rolePermissionMapper.insert(rolePermission);
        }
        return true;
    }
}
